﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace milanov
{
    public class Cartclass
    {
        // Thom
        /// <summary>
        /// methode die met behulp van een username de user zijn ID ophaalt uit de database
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Guid getUserID (string name)
        {
            Guid userID;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                string result = "SELECT UserId FROM [Users] WHERE UserName = @name";
                SqlCommand showresult = new SqlCommand(result, con);
                showresult.Parameters.AddWithValue("name", name);
                con.Open();
                userID = Guid.Parse(Convert.ToString(showresult.ExecuteScalar()));
                con.Close();
            }
            return userID;
        }

        /// <summary>
        /// methode die een orderID terug geeft die nog niet completed is (completed=0)
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static string getOrderid(Guid userID)
        {
            string orderID;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                string result = "SELECT OrderId FROM [Orders] WHERE UserId = @id AND Completed = 'false'";
                SqlCommand showresult = new SqlCommand(result, con);
                showresult.Parameters.AddWithValue("id", userID);
                con.Open();
                orderID = Convert.ToString(showresult.ExecuteScalar());
                con.Close();
            }
            return orderID;
        }
        /// <summary>
        /// methode die een nieuwe order aanmaakt
        /// </summary>
        /// <param name="userID"></param>
        public static bool createOrder(Guid userID)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                string insert = "INSERT INTO [Orders] (UserId) values (@UserId);";
                using (SqlCommand add = new SqlCommand(insert, con))
                {
                    add.Parameters.AddWithValue("@UserId", userID);
                    con.Open();
                    try
                    {
                        add.ExecuteNonQuery();
                        //order aangemaakt
                        return true;
                    }
                    catch
                    {
                        //order niet aangemaakt
                        return false;
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
        }

        public static void AddToCart(int ItemId, string ordernr)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                con.Open();
                string inse = "INSERT INTO [Cart] (OrderId, PhotoId) values (@orderid, @photoid);";
                using (SqlCommand add = new SqlCommand(inse, con))
                {   
                    add.Parameters.AddWithValue("@orderid", ordernr);
                    add.Parameters.AddWithValue("@photoid", ItemId);
                    try
                    {
                        add.ExecuteNonQuery();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
        }
        public static void RemoveFromCart(int ItemId, string ordernr)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                con.Open();
                string inse = "DELETE FROM [Cart] WHERE OrderId = @orderid AND PhotoId = @photoid;";
                using (SqlCommand add = new SqlCommand(inse, con))
                {
                    add.Parameters.AddWithValue("@orderid", ordernr);
                    add.Parameters.AddWithValue("@photoid", ItemId);
                    try
                    {
                        add.ExecuteNonQuery();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
        }
    }
}